<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    图解MySQL里的各种 JOIN.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-mysql/图解MySQL里的各种 JOIN"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  图解MySQL里的各种 JOIN.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/mysql/%E5%9B%BE%E8%A7%A3MySQL%E9%87%8C%E7%9A%84%E5%90%84%E7%A7%8D%20JOIN/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/mysql/">mysql</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.2k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">10 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="图解MySQL里的各种-JOIN"><a href="#图解MySQL里的各种-JOIN" class="headerlink" title="图解MySQL里的各种 JOIN"></a>图解MySQL里的各种 JOIN</h1><p>链接：<a target="_blank" rel="noopener" href="https://mazhuang.org/2017/09/11/joins-in-sql/">https://mazhuang.org/2017/09/11/joins-in-sql/</a></p>
<p>从业以来主要在做客户端，用到的数据库都是表结构比较简单的 SQLite，以我那还给老师一大半的 SQL 水平倒也能对付。现在偶尔需要到后台的 SQL Server 里追查一些数据问题，就显得有点捉襟见肘了，特别是各种 JOIN，有时候傻傻分不清楚，于是索性弄明白并做个记录。  </p>
<h3 id=""><a href="#" class="headerlink" title=""></a></h3><h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a><strong>前言</strong></h3><p>在各种问答社区里谈及 SQL 里的各种 JOIN 之间的区别时，最被广为引用的是 CodeProject 上 C.L. Moffatt 的文章 Visual Representation of SQL Joins，他确实讲得简单明了，使用文氏图来帮助理解，效果明显。本文将沿用他的讲解方式，稍有演绎，可以视为该文较为粗糙的中译版。</p>
<h3 id="约定"><a href="#约定" class="headerlink" title="约定"></a><strong>约定</strong></h3><p>下文将使用两个数据库表 Table_A 和 Table_B 来进行示例讲解，其结构与数据分别如下：</p>
<p>`mysql&gt; SELECT * FROM Table_A ORDER BY PK ASC;<br>+—-+———+<br>| PK | Value   |<br>+—-+———+<br>|  1 | both ab |<br>|  2 | only a  |<br>+—-+———+<br>2 rows in set (0.00 sec)  </p>
<p>mysql&gt; SELECT * from Table_B ORDER BY PK ASC;<br>+—-+———+<br>| PK | Value   |<br>+—-+———+<br>|  1 | both ab |<br>|  3 | only b  |<br>+—-+———+<br>2 rows in set (0.00 sec)<br>`</p>
<p>其中 PK 为 1 的记录在 Table_A 和 Table_B 中都有，2 为 Table_A 特有，3 为 Table_B 特有。</p>
<h3 id="-1"><a href="#-1" class="headerlink" title=""></a></h3><h3 id="常用的-JOIN"><a href="#常用的-JOIN" class="headerlink" title="常用的 JOIN"></a><strong>常用的 JOIN</strong></h3><h2 id="1、INNER-JOIN"><a href="#1、INNER-JOIN" class="headerlink" title="1、INNER JOIN"></a>1、INNER JOIN</h2><p>INNER JOIN 一般被译作内连接。内连接查询能将左表（表 A）和右表（表 B）中能关联起来的数据连接后返回。</p>
<p><strong>文氏图：</strong></p>
<p><img src=""></p>
<p>INNER JOIN</p>
<p><strong>示例查询：</strong></p>
<p><code>SELECT A.PK AS A_PK, B.PK AS B_PK,          A.Value AS A_Value, B.Value AS B_Value   FROM Table_A A   INNER JOIN Table_B B   ON A.PK = B.PK;   </code></p>
<p>查询结果：</p>
<p><code>+------+------+---------+---------+   | A_PK | B_PK | A_Value | B_Value |   +------+------+---------+---------+   |    1 |    1 | both ab | both ab |   +------+------+---------+---------+   1 row in set (0.00 sec)   </code></p>
<p>注：其中 A 为 Table_A 的别名，B 为 Table_B 的别名，下同。</p>
<p><strong>2、LEFT JOIN</strong></p>
<p>LEFT JOIN 一般被译作左连接，也写作 LEFT OUTER JOIN。左连接查询会返回左表（表 A）中所有记录，不管右表（表 B）中有没有关联的数据。在右表中找到的关联数据列也会被一起返回。</p>
<p><strong>文氏图：</strong></p>
<p><img src=""></p>
<p>LEFT JOIN</p>
<p><strong>示例查询：</strong></p>
<p><code>SELECT A.PK AS A_PK, B.PK AS B_PK,          A.Value AS A_Value, B.Value AS B_Value   FROM Table_A A   LEFT JOIN Table_B B   ON A.PK = B.PK;   </code></p>
<p>查询结果：</p>
<p><code>+------+------+---------+---------+   | A_PK | B_PK | A_Value | B_Value |   +------+------+---------+---------+   |    1 |    1 | both ab | both ba |   |    2 | NULL | only a  | NULL    |   +------+------+---------+---------+   2 rows in set (0.00 sec)   </code></p>
<hr>
<h2 id="3、RIGHT-JOIN"><a href="#3、RIGHT-JOIN" class="headerlink" title="3、RIGHT JOIN"></a>3、RIGHT JOIN</h2><p>RIGHT JOIN 一般被译作右连接，也写作 RIGHT OUTER JOIN。右连接查询会返回右表（表 B）中所有记录，不管左表（表 A）中有没有关联的数据。在左表中找到的关联数据列也会被一起返回。</p>
<p><strong>文氏图：</strong></p>
<p><img src=""></p>
<p>RIGHT JOIN</p>
<p><strong>示例查询：</strong></p>
<p><code>SELECT A.PK AS A_PK, B.PK AS B_PK,          A.Value AS A_Value, B.Value AS B_Value   FROM Table_A A   RIGHT JOIN Table_B B   ON A.PK = B.PK;   </code></p>
<p>查询结果：</p>
<p><code>+------+------+---------+---------+   | A_PK | B_PK | A_Value | B_Value |   +------+------+---------+---------+   |    1 |    1 | both ab | both ba |   | NULL |    3 | NULL    | only b  |   +------+------+---------+---------+   2 rows in set (0.00 sec)   </code></p>
<hr>
<h2 id="4、FULL-OUTER-JOIN"><a href="#4、FULL-OUTER-JOIN" class="headerlink" title="4、FULL OUTER JOIN"></a>4、FULL OUTER JOIN</h2><p>FULL OUTER JOIN 一般被译作外连接、全连接，实际查询语句中可以写作 FULL OUTER JOIN 或 FULL JOIN。外连接查询能返回左右表里的所有记录，其中左右表里能关联起来的记录被连接后返回。</p>
<p><strong>文氏图：</strong></p>
<p><img src=""></p>
<p>FULL OUTER JOIN</p>
<p><strong>示例查询：</strong></p>
<p><code>SELECT A.PK AS A_PK, B.PK AS B_PK,          A.Value AS A_Value, B.Value AS B_Value   FROM Table_A A   FULL OUTER JOIN Table_B B   ON A.PK = B.PK;   </code></p>
<p>查询结果：</p>
<p><code>ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#39;FULL OUTER JOIN Table_B B   ON A.PK = B.PK&#39; at line 4   </code></p>
<p>注：我当前示例使用的 MySQL 不支持 FULL OUTER JOIN。</p>
<p>应当返回的结果（使用 UNION 模拟）：</p>
<p><code>mysql&gt; SELECT *       -&gt; FROM Table_A       -&gt; LEFT JOIN Table_B       -&gt; ON Table_A.PK = Table_B.PK       -&gt; UNION ALL       -&gt; SELECT *       -&gt; FROM Table_A       -&gt; RIGHT JOIN Table_B       -&gt; ON Table_A.PK = Table_B.PK       -&gt; WHERE Table_A.PK IS NULL;   +------+---------+------+---------+   | PK   | Value   | PK   | Value   |   +------+---------+------+---------+   |    1 | both ab |    1 | both ba |   |    2 | only a  | NULL | NULL    |   | NULL | NULL    |    3 | only b  |   +------+---------+------+---------+   3 rows in set (0.00 sec)   </code></p>
<hr>
<h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><p>以上四种，就是 SQL 里常见 JOIN 的种类和概念了，看一下它们的合影：</p>
<p><img src=""></p>
<p>小结</p>
<p>有没有感觉少了些什么，学数学集合时完全不止这几种情况？确实如此，继续看。</p>
<h3 id="-2"><a href="#-2" class="headerlink" title=""></a></h3><h3 id="延伸用法"><a href="#延伸用法" class="headerlink" title="延伸用法"></a><strong>延伸用法</strong></h3><h2 id="1、LEFT-JOIN-EXCLUDING-INNER-JOIN"><a href="#1、LEFT-JOIN-EXCLUDING-INNER-JOIN" class="headerlink" title="1、LEFT JOIN EXCLUDING INNER JOIN"></a>1、LEFT JOIN EXCLUDING INNER JOIN</h2><p>返回左表有但右表没有关联数据的记录集。</p>
<p><strong>文氏图：</strong></p>
<p><img src=""></p>
<p>LEFT JOIN EXCLUDING INNER JOIN</p>
<p><strong>示例查询：</strong></p>
<p><code>SELECT A.PK AS A_PK, B.PK AS B_PK,          A.Value AS A_Value, B.Value AS B_Value   FROM Table_A A   LEFT JOIN Table_B B   ON A.PK = B.PK   WHERE B.PK IS NULL;   </code></p>
<p>查询结果：</p>
<p><code>+------+------+---------+---------+   | A_PK | B_PK | A_Value | B_Value |   +------+------+---------+---------+   |    2 | NULL | only a  | NULL    |   +------+------+---------+---------+   1 row in set (0.01 sec)   </code></p>
<hr>
<h2 id="2、RIGHT-JOIN-EXCLUDING-INNER-JOIN"><a href="#2、RIGHT-JOIN-EXCLUDING-INNER-JOIN" class="headerlink" title="2、RIGHT JOIN EXCLUDING INNER JOIN"></a>2、RIGHT JOIN EXCLUDING INNER JOIN</h2><p>返回右表有但左表没有关联数据的记录集。</p>
<p><strong>文氏图：</strong></p>
<p><img src=""></p>
<p>RIGHT JOIN EXCLUDING INNER JOIN</p>
<p><strong>示例查询：</strong></p>
<p><code>SELECT A.PK AS A_PK, B.PK AS B_PK,          A.Value AS A_Value, B.Value AS B_Value   FROM Table_A A   RIGHT JOIN Table_B B   ON A.PK = B.PK   WHERE A.PK IS NULL;   </code></p>
<p>查询结果：</p>
<p><code>+------+------+---------+---------+   | A_PK | B_PK | A_Value | B_Value |   +------+------+---------+---------+   | NULL |    3 | NULL    | only b  |   +------+------+---------+---------+   1 row in set (0.00 sec)   </code></p>
<hr>
<h2 id="3、FULL-OUTER-JOIN-EXCLUDING-INNER-JOIN"><a href="#3、FULL-OUTER-JOIN-EXCLUDING-INNER-JOIN" class="headerlink" title="3、FULL OUTER JOIN EXCLUDING INNER JOIN"></a>3、FULL OUTER JOIN EXCLUDING INNER JOIN</h2><p>返回左表和右表里没有相互关联的记录集。</p>
<p><strong>文氏图：</strong></p>
<p><img src=""></p>
<p>FULL OUTER JOIN EXCLUDING INNER JOIN</p>
<p><strong>示例查询：</strong></p>
<p><code>SELECT A.PK AS A_PK, B.PK AS B_PK,          A.Value AS A_Value, B.Value AS B_Value   FROM Table_A A   FULL OUTER JOIN Table_B B   ON A.PK = B.PK   WHERE A.PK IS NULL   OR B.PK IS NULL;   </code></p>
<p>因为使用到了 FULL OUTER JOIN，MySQL 在执行该查询时再次报错。</p>
<p><code>ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#39;FULL OUTER JOIN Table_B B   ON A.PK = B.PK   WHERE A.PK IS NULL   OR B.PK IS NULL&#39; at line 4   </code></p>
<p>应当返回的结果（用 UNION 模拟）：</p>
<p><code>mysql&gt; SELECT *       -&gt; FROM Table_A       -&gt; LEFT JOIN Table_B       -&gt; ON Table_A.PK = Table_B.PK       -&gt; WHERE Table_B.PK IS NULL       -&gt; UNION ALL       -&gt; SELECT *       -&gt; FROM Table_A       -&gt; RIGHT JOIN Table_B       -&gt; ON Table_A.PK = Table_B.PK       -&gt; WHERE Table_A.PK IS NULL;   +------+--------+------+--------+   | PK   | Value  | PK   | Value  |   +------+--------+------+--------+   |    2 | only a | NULL | NULL   |   | NULL | NULL   |    3 | only b |   +------+--------+------+--------+   2 rows in set (0.00 sec)</code></p>
<h3 id="-3"><a href="#-3" class="headerlink" title=""></a></h3><h3 id="-4"><a href="#-4" class="headerlink" title=""></a></h3><h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a><strong>总结</strong></h3><p>以上七种用法基本上可以覆盖各种 JOIN 查询了。七种用法的全家福：</p>
<p><img src=""></p>
<p>看着它们，我仿佛回到了当年学数学，求交集并集的时代……</p>
<p>顺带张贴一下 C.L. Moffatt 带 SQL 语句的图片，配合学习，风味更佳：</p>
<p><img src=""></p>
<h3 id="-5"><a href="#-5" class="headerlink" title=""></a></h3><h3 id="更多的-JOIN"><a href="#更多的-JOIN" class="headerlink" title="更多的 JOIN"></a><strong>更多的 JOIN</strong></h3><p>除以上几种外，还有更多的 JOIN 用法，比如 CROSS JOIN（迪卡尔集）、SELF JOIN，可以参考 SQL JOINS Slide Presentation 学习。</p>
<h2 id="1、CROSS-JOIN"><a href="#1、CROSS-JOIN" class="headerlink" title="1、CROSS JOIN"></a>1、CROSS JOIN</h2><p>返回左表与右表之间符合条件的记录的迪卡尔集。</p>
<p><strong>图示：</strong></p>
<p><img src=""></p>
<p><strong>示例查询：</strong></p>
<p><code>SELECT A.PK AS A_PK, B.PK AS B_PK,          A.Value AS A_Value, B.Value AS B_Value   FROM Table_A A   CROSS JOIN Table_B B;   </code></p>
<p><strong>查询结果：</strong></p>
<p><code>+------+------+---------+---------+   | A_PK | B_PK | A_Value | B_Value |   +------+------+---------+---------+   |    1 |    1 | both ab | both ba |   |    2 |    1 | only a  | both ba |   |    1 |    3 | both ab | only b  |   |    2 |    3 | only a  | only b  |   +------+------+---------+---------+   4 rows in set (0.00 sec)   </code></p>
<p>上面讲过的几种 JOIN 查询的结果都可以用 CROSS JOIN 加条件模拟出来，比如 INNER JOIN 对应 CROSS JOIN … WHERE A.PK = B.PK。</p>
<h2 id="2、SELF-JOIN"><a href="#2、SELF-JOIN" class="headerlink" title="2、SELF JOIN"></a>2、SELF JOIN</h2><p>返回表与自己连接后符合条件的记录，一般用在表里有一个字段是用主键作为外键的情况。</p>
<p>比如 Table_C 的结构与数据如下：</p>
<p><code>+--------+----------+-------------+   | EMP_ID | EMP_NAME | EMP_SUPV_ID |   +--------+----------+-------------+   |   1001 | Ma       |        NULL |   |   1002 | Zhuang   |        1001 |   +--------+----------+-------------+   2 rows in set (0.00 sec)   </code></p>
<p>EMP_ID 字段表示员工 ID，EMP_NAME 字段表示员工姓名，EMP_SUPV_ID 表示主管 ID。</p>
<p><strong>示例查询：</strong></p>
<p>现在我们想查询所有有主管的员工及其对应的主管 ID 和姓名，就可以用 SELF JOIN 来实现。</p>
<p><code>SELECT A.EMP_ID AS EMP_ID, A.EMP_NAME AS EMP_NAME,       B.EMP_ID AS EMP_SUPV_ID, B.EMP_NAME AS EMP_SUPV_NAME   FROM Table_C A, Table_C B   WHERE A.EMP_SUPV_ID = B.EMP_ID;   </code></p>
<p>查询结果：</p>
<p><code>+--------+----------+-------------+---------------+   | EMP_ID | EMP_NAME | EMP_SUPV_ID | EMP_SUPV_NAME |   +--------+----------+-------------+---------------+   |   1002 | Zhuang   |        1001 | Ma            |   +--------+----------+-------------+---------------+   1 row in set (0.00 sec)</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p><strong>——End——</strong></p>
<h4 id="-6"><a href="#-6" class="headerlink" title=""></a></h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">后台回复关键字：**1024**，获取一份精心整理的技术干货</span><br><span class="line"></span><br><span class="line">后台回复关键字：**进群**，带你进入高手如云的交流群。</span><br><span class="line"></span><br><span class="line">**推荐阅读**</span><br><span class="line"></span><br><span class="line">*   [HR 黑话大全：那些扎心的潜台词](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzA3MTg4NjY4Mw&#x3D;&#x3D;&amp;mid&#x3D;2457311385&amp;idx&#x3D;1&amp;sn&#x3D;2063597eb1184681c19f9b75d8521381&amp;chksm&#x3D;88a58aadbfd203bb67db676262d1d747a376cbeb6d94f5f4df332b64fbf6df28dbe3d07dc2bb&amp;scene&#x3D;21#wechat_redirect)  </span><br><span class="line">    </span><br><span class="line">*   [百度网盘突然大调整，网友炸了..](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzA3MTg4NjY4Mw&#x3D;&#x3D;&amp;mid&#x3D;2457311371&amp;idx&#x3D;1&amp;sn&#x3D;a1a801ffe5be12ae3741be7addda34b8&amp;chksm&#x3D;88a58abfbfd203a943e7209c306f22db9182402cda7222588cb161fa81900a680557f7374efd&amp;scene&#x3D;21#wechat_redirect)  </span><br><span class="line">    </span><br><span class="line">*   [大公司病，太形象了](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzA3MTg4NjY4Mw&#x3D;&#x3D;&amp;mid&#x3D;2457311283&amp;idx&#x3D;1&amp;sn&#x3D;443f1dbaee9bdd15676136df6322dfc0&amp;chksm&#x3D;88a58a07bfd203111fa91660ab0b05111d5dca6dc86cf2b34d555d3c9a64769cd4d3ce426601&amp;scene&#x3D;21#wechat_redirect)  </span><br><span class="line">    </span><br><span class="line">*   [阿里规定超过3张表，禁止JOIN，为何？](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzA3MTg4NjY4Mw&#x3D;&#x3D;&amp;mid&#x3D;2457311203&amp;idx&#x3D;1&amp;sn&#x3D;6fa4c2a34411dd55cf641056008485bd&amp;chksm&#x3D;88a585d7bfd20cc1a439faeaaf16490380c34493d10fb12f9989c937d76d642c602fb09ad1a4&amp;scene&#x3D;21#wechat_redirect)  </span><br><span class="line">    </span><br><span class="line">*   [MySQL 常用命令手册](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzA3MTg4NjY4Mw&#x3D;&#x3D;&amp;mid&#x3D;2457311153&amp;idx&#x3D;2&amp;sn&#x3D;b897d90a3edd121eb02681208bee6b72&amp;chksm&#x3D;88a58585bfd20c930998280b4ef34511b43bc52dcb079ee3427cdce7c5d06c1f79ae75a8cf93&amp;scene&#x3D;21#wechat_redirect)  </span><br><span class="line">    </span><br><span class="line">*   [5个免费在线 SQL 数据库环境](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzA3MTg4NjY4Mw&#x3D;&#x3D;&amp;mid&#x3D;2457311071&amp;idx&#x3D;2&amp;sn&#x3D;b54a71012a5cca8857ac923a0beebf04&amp;chksm&#x3D;88a5856bbfd20c7de032b57034e61e62f6d819575171fb64cb514f5d717ae74e0de03ed834c8&amp;scene&#x3D;21#wechat_redirect)</span><br><span class="line">    </span><br><span class="line">      </span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">**这是一个能学到技术的公众号，欢迎关注**</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src=""></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">觉得不错，记得帮忙点下『**在看**』↓↓↓</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/mysql/%E5%9B%BE%E8%A7%A3MySQL%E9%87%8C%E7%9A%84%E5%90%84%E7%A7%8D%20JOIN/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/mysql/" rel="tag">mysql</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/redis/%E4%B8%87%E5%AD%97%E9%95%BF%E6%96%87%EF%BC%81%E6%90%9E%E6%87%82%E8%BF%99%E4%BA%9BRedis%E7%9F%A5%E8%AF%86%E7%82%B9%EF%BC%8C%E5%90%8A%E6%89%93%E9%9D%A2%E8%AF%95%E5%AE%98/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            万字长文！搞懂这些Redis知识点，吊打面试官.md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/mysql/02.Dbeaver%E5%8A%9F%E8%83%BD%E5%BC%BA%E5%A4%A7%E8%BF%98%E5%85%8D%E8%B4%B9/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">02.Dbeaver功能强大还免费.md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>